/**
 * Controls wich user we're dealing with
 */
var login = {
	/**
	 * String username
	 */
	user: '',
	
	/**
	* Do a ajax request to get the list of teachers and putting it in an dropdownlist.
	* @param Object userList	The results returned in Json format.
	*/	
	getTeacherList: function() {
		ajax(Woordenschat.url + "php/?task=getPupils&userid=0", function(userList){
			var panel = content.document.getElementById('piraat-panel');
			var userMenu = login.createMenu('piraat-teacherMenuList', userList);
			userMenu.onchange = function() {
				login.getUserList(this.value);
			}
			var div = content.document.createElement("div");
			div.id = 'piraat-teacherMenuDiv';
			div.appendChild(document.createTextNode('Leraar'));
			div.appendChild(content.document.createElement('br'));
			div.appendChild(userMenu);
			panel.appendChild(div);
		});
	},
	
	/**
	* Same as getTeacherList, except the pupils are being requested and displayed.
	* @param Int teacherId	The id of the teacher that is requested
	*/
	getUserList: function(teacherId) {
		ajax(Woordenschat.url + "php/?task=getPupils&userid=" + teacherId, function(userList){
			var panel = content.document.getElementById('piraat-panel');
			var userMenu = login.createMenu('piraat-pupilMenuList', userList);
			var div = content.document.createElement("div");
			div.id = 'piraat-pupilMenuDiv';
			div.appendChild(document.createTextNode('naam'));
			div.appendChild(content.document.createElement('br'));
			div.appendChild(userMenu);
			panel.appendChild(div);
		});
	},
	
	/**
	* Creating a dropdownlist with the given information.
	* @param String menuId	The id of the menu.
	* @param Object	data	The list of users containing either teachers or pupils.		
	*/
	createMenu: function(menuId, data) {
		if(content.document.getElementById('piraat-pupilMenuDiv')){
			var child = content.document.getElementById('piraat-pupilMenuDiv');
			var parent = child.parentNode;
			parent.removeChild(child);
		}
		var userMenu = content.document.createElement("select");
		userMenu.id = menuId;
		var option = content.document.createElement('option');
		option.text = '* selecteer *';
		userMenu.appendChild(option);
		for(var i = 0; i < data.result.length; i++){
			var option = content.document.createElement('option');
			option.text = data.result[i].username;
			option.value = data.result[i].id;
			userMenu.appendChild(option);
		}
		return userMenu;
		
	},
	
	/**
	* Starts a session under the name of the chosen pupil.
	* @param String login.user	The name of the pupil chosen by the User in the dropdownlist.
	*/
	sendName: function(){
		var menu = content.document.getElementById('piraat-pupilMenuList');
		var selected = menu.selectedIndex;
		login.user = menu.options[selected].text;
		
		ajax(Woordenschat.url + "php/?task=login&username="+login.user, function(data){
		});
	}
}